package yxy.game.pm2.command.union;

import org.joda.time.DateTime;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.module.RedisLock;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionMessage;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.UnionJoinRule;
import yxy.game.pm2.module.union.player.UnionPlayer;

/** 联盟 联盟申请加入 */
@MessageType(proto = Command.UnionApply, classtype = UnionMessage.UnionApplyRequest.class)
public class CMD_UnionApply extends Command<UnionMessage.UnionApplyRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionMessage.UnionApplyRequest request) {
		String playerCode = messageSource.getTag();
		String unionCode = request.getCode();// 盟code

		UnionMessage.UnionApplyResponse.Builder response = UnionMessage.UnionApplyResponse.newBuilder();

		RedisLock uLock = UnionBean.lock(unionCode);
		RedisLock pLock = UnionPlayer.lock(playerCode);
		try {
			if (uLock.tryLock() && pLock.tryLock()) {

				UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
				Result result = new UnionHandle().checkPlayerUnion(entry);
				if (!result.equals(Result.UnionOut)) {
					send(messageSource, response, result);
					return;
				}
				UnionPlayer unionPlayer = entry.getUnionPlayer();

				// 禁止时间
				if (unionPlayer.isforbiden()) {
					long cd = unionPlayer.getForbidTime().getMillis() - DateTime.now().getMillis();
					cd = cd < 1000 ? 1000 : cd;
					response.setCd(cd);
					send(messageSource, response, Result.UnionJoinForbid);// 尚未解禁 你退出上一个联盟的冷静期还没有结束
					return;
				}

				UnionBean union = UnionBean.get(unionCode);
				if (union == null) {
					send(messageSource, response, Result.UnionIsDissolved);// 联盟已经解散了
					return;
				}
				PlayerBean playerBean = PlayerBean.get(playerCode);
				// 区域判断
				boolean areaChecker = new UnionHandle().checkArea(union, playerBean);
				if (!areaChecker) {
					send(messageSource, response, Result.ParamError);// TODO 你不能加入这个联盟
					return;
				}
				// 入盟规则判断
				UnionJoinRule joinRule = union.getJoinRule();
				// 等级条件判断
				switch (joinRule.getCondition()) {
				case LEVEL:
					if (playerBean.getLevel() < joinRule.getValue()) {
						send(messageSource, response, Result.UnionJoinLowLevel);// 等级不够
						return;
					}
					break;
				case POWER:
					if (playerBean.getPower() < joinRule.getValue()) {
						send(messageSource, response, Result.UnionJoinLowPower);// 战力不够
						return;
					}
					break;
				}

				if (joinRule.isAudit()) {
					if (!union.getApplyList().contains(playerCode)) {
						union.getApplyList().add(playerCode);
						union.set();
					}
					send(messageSource, response, Result.Success);// 申请成功
					return;
				}

				// 玩家进联盟操作
				result = new UnionHandle().addMember(union, unionPlayer);
				send(messageSource, response, result);
				if (!result.equals(Result.Success)) {
					return;
				}

				UnionMessage.UnionMainResponse.Builder unionMessage = new UnionHandle().unionMain(union, playerCode);
				MessageEmitterHandler.getInstance().send(messageSource, unionMessage.build());

			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			uLock.unlock();
			pLock.unlock();
		}
	}

	private void send(MessageSource messageSource, UnionMessage.UnionApplyResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}